package com.eibus.applicationconnector.dotnet;


import com.eibus.util.CordysConstants;
import com.eibus.xml.nom.Node;
import com.eibus.xml.xpath.XPath;
import com.eibus.xml.xpath.XPathMetaInfo;

public class XPathUtil
{
    
    static final XPath tupleXPathUnderSOAPBody = new XPath("/Envelope/Body/*/tuple[1]");
    
    static final XPath entryXPath = new XPath(".//entry[1]");
    
    static final XPath soapFaultXPath = new XPath(".//" + CordysConstants.CORDYS_DEFAULT_SOAP_PREFIX + ":Fault");
    
    static final XPath soapFaultstringXPath = new XPath(".//faultstring");
    
    static final XPath soapFaultcodeXPath = new XPath(".//faultcode");
    
    static final XPathMetaInfo soapFaultMeta  = new XPathMetaInfo();
    
    static
    {
        soapFaultMeta.addNamespaceBinding(CordysConstants.CORDYS_DEFAULT_SOAP_PREFIX, CordysConstants.SOAP11_ENVELOPE_NAMESPACE);
    }
    
    static int getSOAPFault(int node)
    {
        return soapFaultXPath.firstMatch(node, XPathUtil.soapFaultMeta);
    }
    
    static int getSOAPFaultstring(int node)
    {
        return soapFaultstringXPath.firstMatch(node, null);
    }
    
    static int getSOAPFaultcode(int node)
    {
        return soapFaultcodeXPath.firstMatch(node, null);
    }
    
    static int getTuple(int node)
    {
        return tupleXPathUnderSOAPBody.firstMatch(node, null);
    }
    
    static String[] getXPathData(int node, XPath xPath)
    {
        int[] nodes = xPath.selectElementNodes(node);
        String[] data = new String[nodes.length];
        for(int i = 0; i < data.length; i++)
        {
            data[i] = Node.getData(nodes[i]);
        }
        
        return data;
    }
}